package com.android.clinic.app.ui.doctor;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.clinic.app.ClinicApplication;
import com.android.clinic.app.R;
import com.android.clinic.app.adapter.BaseListAdapter;
import com.android.clinic.app.adapter.ReportAdapter;
import com.android.clinic.app.base.BaseRefreshListActivity;
import com.android.clinic.app.entity.ReportInfo;
import com.android.clinic.app.http.HttpClient;
import com.android.clinic.app.http.base.BaseObserver;
import com.android.clinic.app.http.utils.RxObservableUtils;
import com.kennyc.view.MultiStateView;

import java.util.List;

/**
 * 我的报告
 * Created by cheng on 2018/7/17.
 */
public class MyReportListActivity extends BaseRefreshListActivity<ReportInfo> {

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_refresh_listview;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBarTitle("我的报告");
        setTitleBarLeftResource(R.drawable.icon_back);

        mAdapter.setOnItemClickListener((view, position) -> {
            Intent intent = new Intent(MyReportListActivity.this, MyReportDetailActivity.class);
            intent.putExtra("reportInfo", mAdapter.getItem(position));
            startActivity(intent);
        });
    }

    @Override
    protected void reloadData() {
        HttpClient.getHttpService().getListByUser(ClinicApplication.getUserInfo().getId())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<List<ReportInfo>>() {
                    @Override
                    public void onSuccess(List<ReportInfo> reportInfoList) {
                        refreshComplete();
                        if (reportInfoList.size() > 0) {
                            setMultiStateView(MultiStateView.VIEW_STATE_CONTENT);
                            for (ReportInfo reportInfo : reportInfoList)
                                mAdapter.add(reportInfo);
                        } else {
                            setMultiStateView(MultiStateView.VIEW_STATE_EMPTY, "暂无报告!");
                        }
                    }

                    @Override
                    public void onFailure(String message) {
                        setMultiStateView(MultiStateView.VIEW_STATE_EMPTY);
                        refreshComplete();
                        showToast(message);
                    }
                });
    }

    @Override
    protected BaseListAdapter<ReportInfo> getAdapter() {
        return new ReportAdapter(this);
    }
}
